package com.itheima.bos.service.take_delivery.impl;

import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.index.query.WildcardQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itheima.bos.dao.base.WayBillRepositoey;
import com.itheima.bos.domain.take_delivery.WayBill;
import com.itheima.bos.index.WayBillIndexRepositoey;
import com.itheima.bos.service.take_delivery.IWayBillService;

@Service
@Transactional
public class WayBillServiceImpl implements IWayBillService {

	@Autowired
	private WayBillRepositoey wayBillRepositoey;
	
	@Autowired
	private WayBillIndexRepositoey wayBillIndexRepositoey;

	// 保存运单的方法
	@Override
	public void save(WayBill waybill) {
		wayBillRepositoey.save(waybill);

	}

	@Override
	public Page findAll(WayBill waybill, Pageable pageable) {
		// 判断传入的参数是否存在
		if (StringUtils.isBlank(waybill.getWayBillNum())
				&& StringUtils.isBlank(waybill.getSendAddress())
				&& StringUtils.isBlank(waybill.getRecAddress())
				&& (waybill.getSignStatus() == null || waybill.getSignStatus() == 0)) {
			// 无条件查询,就查询全部
			return wayBillRepositoey.findAll(pageable);
		} else {
			// 条件查询

			BoolQueryBuilder query = new BoolQueryBuilder();// 布尔条件查询,多条件组合查询

			// 运单条件
			if (StringUtils.isNoneBlank(waybill.getWayBillNum())) {
				// 创建等值查询条件
				TermQueryBuilder termQueryBuilder = new TermQueryBuilder(
						"wayBillNum", waybill.getWayBillNum());
				query.must(termQueryBuilder);
			}

			// 发货地条件,模糊的条件
			if (StringUtils.isNoneBlank(waybill.getSendAddress())) {
				WildcardQueryBuilder queryBuilder = new WildcardQueryBuilder(
						"sendAddress", "*" + waybill.getSendAddress() + "*");
				query.must(queryBuilder);
			}

			// 收货地查询,模糊查询对象
			if (StringUtils.isNoneBlank(waybill.getRecAddress())) {
				WildcardQueryBuilder queryBuilder = new WildcardQueryBuilder(
						"recAddress", "*" + waybill.getRecAddress() + "*");
				query.must(queryBuilder);

			}
			// 输运类型,等值查询
			if (StringUtils.isNoneBlank(waybill.getSendProNum())) {
				// 创建等值查询条件
				TermQueryBuilder termQueryBuilder = new TermQueryBuilder(
						"sendProNum", waybill.getSendProNum());
				query.must(termQueryBuilder);
			}

			// 查询运输状态
			if (waybill.getSignStatus() != null && waybill.getSignStatus() != 0) {
				// 创建等值查询条件
				TermQueryBuilder termQueryBuilder = new TermQueryBuilder(
						"signStatus", waybill.getSignStatus());
				query.must(termQueryBuilder);
			}
			
			SearchQuery searchQuery = new NativeSearchQuery(query);
																										
			searchQuery.setPageable(pageable);// 分頁的總條件
			
			return wayBillIndexRepositoey.search(searchQuery);
		}
		
	}

	@Override
	public WayBill findByWayBillNum(String wayBillNum) {

		return wayBillRepositoey.findByWayBillNum(wayBillNum);
	}

}
